import pandas as pd


df = pd.read_csv('./newdata/data.csv', encoding='utf-8')
df_movie = pd.read_csv('./newdata/movie.csv', encoding='utf-8')
df_director = pd.read_csv('./newdata/director.csv', encoding='utf-8')
df_screenwriter = pd.read_csv('./newdata/screenwriter.csv', encoding='utf-8')
df_actor = pd.read_csv('./newdata/actor.csv', encoding='utf-8')
df_type = pd.read_csv('./newdata/type.csv', encoding='utf-8')
df_area = pd.read_csv('./newdata/area.csv', encoding='utf-8')
df_language = pd.read_csv('./newdata/language.csv', encoding='utf-8')

movie_directors, movie_screenwriters, movie_actors, movie_types, movie_areas, movie_languages, \
director_screenwriters, director_actors, director_types, director_areas, \
screenwriter_types, actor_types, actor_languages = [], [], [], [], [], [], [], [], [], [], [], [], []

for index, row in df.iterrows():
    # print(row)
    m_name = row['m_name']
    director = row['director']
    screenwriter = row['screenwriter']
    actor = row['actor']
    type = row['type']
    area = row['area']
    language = row['language']

    screenwriterList = screenwriter.split('/')
    actorList = actor.split('/')
    typeList = type.split('/')
    languageList = language.split('/')

    movieID = df_movie['index:ID'].loc[df_movie['movie'] == m_name].values[0]

    directorID = df_director['index:ID'].loc[df_director['director'] == director].values[0]
    movie_director = [movieID, directorID, 'direct', 'direct']
    movie_directors.append(movie_director)

    for sw in screenwriterList:
        if sw:
            screenwriterID = df_screenwriter['index:ID'].loc[df_screenwriter['screenwriter'] == sw].values[0]
            movie_screenwriter = [movieID, screenwriterID, 'write', 'write']
            movie_screenwriters.append(movie_screenwriter)

    for act in actorList:
        if act:
            actorID = df_actor['index:ID'].loc[df_actor['actor'] == act].values[0]
            movie_actor = [movieID, actorID, 'act_in', 'act_in']
            movie_actors.append(movie_actor)

    for ty in typeList:
        typeID = df_type['index:ID'].loc[df_type['type'] == ty].values[0]
        movie_type = [movieID, typeID, 'is', 'is']
        movie_types.append(movie_type)

    areaID = df_area['index:ID'].loc[df_area['area'] == area].values[0]
    movie_area = [movieID, areaID, 'release_in', 'release_in']
    movie_areas.append(movie_area)

    for lan in languageList:
        if lan:
            languageID = df_language['index:ID'].loc[df_language['language'] == lan].values[0]
            movie_language = [movieID, languageID, 'language_in', 'language_in']
            movie_languages.append(movie_language)

    for sw in screenwriterList:
        if sw:
            screenwriterID = df_screenwriter['index:ID'].loc[df_screenwriter['screenwriter'] == sw].values[0]
            director_screenwriter = [directorID, screenwriterID, 'dir_sw_cooperate', 'dir_sw_cooperate']
            director_screenwriters.append(director_screenwriter)

    for act in actorList:
        if act:
            actorID = df_actor['index:ID'].loc[df_actor['actor'] == act].values[0]
            director_actor = [directorID, actorID, 'dir_act_cooperate', 'dir_act_cooperate']
            director_actors.append(director_actor)

    for ty in typeList:
        typeID = df_type['index:ID'].loc[df_type['type'] == ty].values[0]
        director_type = [directorID, typeID, 'dir_type', 'dir_type']
        director_types.append(director_type)

    director_area = [directorID, areaID, 'dir_in', 'dir_in']
    director_areas.append(director_area)

    for sw in screenwriterList:
        if sw:
            screenwriterID = df_screenwriter['index:ID'].loc[df_screenwriter['screenwriter'] == sw].values[0]
            for ty in typeList:
                typeID = df_type['index:ID'].loc[df_type['type'] == ty].values[0]
                screenwriter_type = [screenwriterID, typeID, 'write_type', 'write_type']
                screenwriter_types.append(screenwriter_type)

    for act in actorList:
        if act:
            actorID = df_actor['index:ID'].loc[df_actor['actor'] == act].values[0]
            for ty in typeList:
                typeID = df_type['index:ID'].loc[df_type['type'] == ty].values[0]
                actor_type = [actorID, typeID, 'act_type', 'act_type']
                actor_types.append(actor_type)

    for act in actorList:
        if act:
            actorID = df_actor['index:ID'].loc[df_actor['actor'] == act].values[0]
            for lan in languageList:
                if lan:
                    languageID = df_language['index:ID'].loc[df_language['language'] == lan].values[0]
            actor_language = [actorID, languageID, 'speak', 'speak']
            actor_languages.append(actor_language)

df_movie_director = pd.DataFrame(data=movie_directors, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_movie_director.to_csv('./newdata/movie_director.csv', index=False, encoding='utf-8_sig')
print('电影-导演关系导出到CSV成功')

df_movie_screenwriter = pd.DataFrame(data=movie_screenwriters, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_movie_screenwriter.to_csv('./newdata/movie_screenwriter.csv', index=False, encoding='utf-8_sig')
print('电影-编剧关系导出到CSV成功')

df_movie_actor = pd.DataFrame(data=movie_actors, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_movie_actor.to_csv('./newdata/movie_actor.csv', index=False, encoding='utf-8_sig')
print('电影-演员关系导出到CSV成功')

df_movie_type = pd.DataFrame(data=movie_types, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_movie_type.to_csv('./newdata/movie_type.csv', index=False, encoding='utf-8_sig')
print('电影-类型关系导出到CSV成功')

df_movie_area = pd.DataFrame(data=movie_areas, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_movie_area.to_csv('./newdata/movie_area.csv', index=False, encoding='utf-8_sig')
print('电影-地区关系导出到CSV成功')

df_movie_language = pd.DataFrame(data=movie_languages, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_movie_language.to_csv('./newdata/movie_language.csv', index=False, encoding='utf-8_sig')
print('电影-语言关系导出到CSV成功')

df_director_screenwriter = pd.DataFrame(data=director_screenwriters, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_director_screenwriter.to_csv('./newdata/director_screenwriter.csv', index=False, encoding='utf-8_sig')
print('导演-编剧关系导出到CSV成功')

df_director_actor = pd.DataFrame(data=director_actors, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_director_actor.to_csv('./newdata/director_actor.csv', index=False, encoding='utf-8_sig')
print('导演-演员关系导出到CSV成功')

df_director_type = pd.DataFrame(data=director_types, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_director_type.to_csv('./newdata/director_type.csv', index=False, encoding='utf-8_sig')
print('导演-类型关系导出到CSV成功')

df_director_area = pd.DataFrame(data=director_areas, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_director_area.to_csv('./newdata/director_area.csv', index=False, encoding='utf-8_sig')
print('导演-地区关系导出到CSV成功')

df_screenwriter_type = pd.DataFrame(data=screenwriter_types, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_screenwriter_type.to_csv('./newdata/screenwriter_type.csv', index=False, encoding='utf-8_sig')
print('编剧-类型关系导出到CSV成功')

df_actor_type = pd.DataFrame(data=actor_types, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_actor_type.to_csv('./newdata/actor_type.csv', index=False, encoding='utf-8_sig')
print('演员-类型关系导出到CSV成功')

df_actor_language = pd.DataFrame(data=actor_languages, columns=[':START_ID', ':ENF_ID', 'relation', ':TYPE'])
df_actor_language.to_csv('./newdata/actor_language.csv', index=False, encoding='utf-8_sig')
print('演员-语言关系导出到CSV成功')
